import axios from "axios";
let token = "";
let port = 0;

const req = axios.create({
  baseURL: `https://127.0.0.1:${port}`,
  timeout: 5000,

  auth: {
    username: "riot",
    password: token,
  },
});

// 请求拦截器
req.interceptors.request.use((config) => {
  config.baseURL = localStorage.getItem("baseURL") as string;

  config.auth!.password = localStorage.getItem("token") as string;

  return config;
});

// 相应拦截器
req.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (error) => {
    let config = error.config;

    if (error && error.response && error.response.data) {
      return { error: error.response.data };
    } else if (config && config.retry) {
      return { error: "请求超时" };
    } else {
      return { error };
    }
  }
);

export default req;
